package com.winderinfo.yikaotianxia.ui.setup;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class FeedbackActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.fee_yijian)
    EditText feeYijian;
    @BindView(R.id.fee_email)
    EditText feeEmail;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

    @OnClick({R.id.fee_back, R.id.fee_wc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fee_back:
                finish();
                break;
            case R.id.fee_wc:
                String text = feeYijian.getText().toString();
                String email = feeEmail.getText().toString();
                //判断空
                if (TextUtils.isEmpty(text) || TextUtils.isEmpty(email)) {
                    ToastUtil.showSuccess(this, "不能为空");
                    return;
                }
                //请求反馈接口
                /**
                 * @id 用户Id
                 * @content 反馈内容
                 * @contact 联系方式
                 * */
                String string = spUtils.getString("UserID", null);
                map.clear();
                map.put("userId", 1);
                map.put("content", text);
                map.put("contact", email);
                presenter.onPostRequest(Api.FEEFBACK, map, RegisterBean.class);
                break;
        }
    }

    @Override
    public void onSuccess(Object data) {
        //反馈数据
        if (data instanceof RegisterBean) {
            //反馈失败
            if (((RegisterBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
            //反馈成功
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }
}
